//
//  IdentifyingController.m
//  i5db
//
//  Created by iMuse on 15-5-19.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "IdentifyingController.h"
#import "PasswordController.h"
#import "AppCenter.h"
#import <SMS_SDK/SMS_SDK.h>

@interface IdentifyingController ()
{
    UILabel * indenLabel ;
    
    UILabel * topLine;
    UILabel * bottomLine;
    
    UIView  * messageView;
    UILabel *  centerLine;
    
    UIButton * indenButton;
    NSInteger  time;
    
    NSTimer * timer;
    
    NSString * phoneNumber;
    UIButton * nextStepButton;
    
    UITextField * indenField;
}

@end

@implementation IdentifyingController

- (id)initWithPhone:(NSString*) phone
{
    self = [super init];
    if (self) {
        self.title = @"填写验证码";
        phoneNumber = phone;
    }
    return self;
}

-(void)updateViewConstraints
{
    [super updateViewConstraints];
    [indenLabel autoPinToTopLayoutGuideOfViewController:self withInset:10];
    [indenLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [indenLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
    [messageView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:indenLabel withOffset:10];
    [messageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [messageView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [messageView autoSetDimension:ALDimensionHeight toSize:40];
    
    [topLine autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:indenLabel withOffset:10];
    [topLine autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [topLine autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [topLine autoSetDimension:ALDimensionHeight toSize:0.5];
    
    [bottomLine autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:messageView withOffset:0];
    [bottomLine autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [bottomLine autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [bottomLine autoSetDimension:ALDimensionHeight toSize:0.5];
    
    [indenField autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
    [indenField autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
    [indenField autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
    [indenField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:100];
    
    [centerLine autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
    [centerLine autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
    [centerLine autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:indenField withOffset:0];
    [centerLine autoSetDimension:ALDimensionWidth toSize:0.5];
    
    [indenButton autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0];
    [indenButton autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
    [indenButton autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:indenField withOffset:0];
    [indenButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    
    [nextStepButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:messageView withOffset:10];
    [nextStepButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
    [nextStepButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20];
    [nextStepButton autoSetDimension:ALDimensionHeight toSize:40];
    
}
-(void) initNavigationBar{
    [super initNavigationBar];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setNeedsUpdateConstraints];
    [self initNavigationBar];
    [self initView];
}
-(void) initView
{
    
    time = 59;
    NSString* string = [NSString stringWithFormat:@"我们已经给你的手机号码+86-%@发送了一条验证短信。",phoneNumber];
    
    NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:string];
    [attrString addAttribute:NSForegroundColorAttributeName value:[AppCenter color:@"133,133,133,1"] range:NSMakeRange(0, 11)];
    [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(11, 15)];
    
    [attrString addAttribute:NSForegroundColorAttributeName value:[AppCenter color:@"133,133,133,1"] range:NSMakeRange(26,string.length-26)];
    
    
    indenLabel = [UILabel newAutoLayoutView];
    indenLabel.numberOfLines  = 0;
    indenLabel.attributedText = attrString;
    indenLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:indenLabel];
    
    messageView = [UIView newAutoLayoutView];
   
    messageView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:messageView];
    
    topLine = [UILabel newAutoLayoutView];
    topLine.backgroundColor = [AppCenter color:@"188,188,188,1"];
    [self.view addSubview:topLine];
    
    bottomLine = [UILabel newAutoLayoutView];
    bottomLine.backgroundColor = [AppCenter color:@"188,188,188,1"];
    [self.view addSubview:bottomLine];
    
    indenField = [UITextField newAutoLayoutView];
    indenField.keyboardType = UIKeyboardTypeNumberPad;
    indenField.clearButtonMode = UITextFieldViewModeWhileEditing;
    indenField.font = [UIFont systemFontOfSize:12];
    indenField.placeholder = @"请输入短信验证码";
    [self performSelector:@selector(textResponder) withObject:nil afterDelay:0];
    [messageView addSubview:indenField];
    
    centerLine = [UILabel newAutoLayoutView];
    centerLine.backgroundColor = [AppCenter color:@"188,188,188,1"];
    [messageView addSubview:centerLine];
    
    indenButton = [UIButton newAutoLayoutView];

    [indenButton addTarget:self action:@selector(refushButton:) forControlEvents:UIControlEventTouchUpInside];
    indenButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [indenButton setTitle:[NSString stringWithFormat:@"%d s",time] forState:UIControlStateNormal];
    [indenButton setTitleColor:[AppCenter color:@"188,188,188,1"] forState:UIControlStateNormal];
    [messageView addSubview:indenButton];
    
    [self startTime];
    
    
    nextStepButton = [UIButton newAutoLayoutView];
    nextStepButton.layer.borderWidth = 1;
    nextStepButton.layer.cornerRadius = 4;
    nextStepButton.layer.borderColor = [AppCenter color:@"76,187,248,1"].CGColor;
    [nextStepButton setTitleColor:[AppCenter color:@"76,187,248,1"] forState:UIControlStateNormal];
    [nextStepButton setTitle:@"下一步" forState:UIControlStateNormal];
    [nextStepButton addTarget:self action:@selector(nextStepButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextStepButton];
    
}
-(void) startTime
{
     timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeSend:) userInfo:nil repeats:YES];
}
-(void)timeSend:(NSTimer*)_time
{
    time = time-1;
    
    if (time<=0)
    {
        [indenButton setTitle:@"重新发送" forState:UIControlStateNormal];
        [indenButton setTitleColor:[AppCenter color:@"76,187,248,1"] forState:UIControlStateNormal];
        [timer invalidate];
        
    }else
    {
        [indenButton setTitleColor:[AppCenter color:@"188,188,188,1"] forState:UIControlStateNormal];
        [indenButton setTitle:[NSString stringWithFormat:@"%d s",time] forState:UIControlStateNormal];
    }
   
    
}

-(void)textResponder
{
    [indenField becomeFirstResponder];
}
-(void)refushButton:(UIButton*)button
{
    if (![button.titleLabel.text isEqualToString:@"重新发送"])
    {
        return;
    }
    
 
    
    [SMS_SDK getVerificationCodeBySMSWithPhone:phoneNumber zone:@"86" result:^(SMS_SDKError *error) {
        
        if (error)
        {
            NSLog(@"%zi,%@",error.errorCode,error.errorDescription);
        }else
        {
            time = 60;
            [self startTime];
            
            
        }
        
    }];
    
}

-(void)nextStepButton:(UIButton*)button
{
    //验证短信验证码
    [self verifyCode];
  
    
}
-(void) verifyCode
{
   
    [SMS_SDK commitVerifyCode:indenField.text result:^(enum SMS_ResponseState state) {
        if (state ==1)
        {
            [timer invalidate];
            PasswordController * controller = [[PasswordController alloc] init];
            [self.navigationController pushViewController:controller animated:YES];
        }else
        {
            [NotificationNavView showMessage:@"验证码输入错误"];
        }
        
    }];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}


@end
